package com.customization.api.e9sso.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.*;

public class GetRequestBaseInfoCmd   extends AbstractCommonCommand<Map<String,Object>> {


    public GetRequestBaseInfoCmd(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;

    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        apidatas.put("status",false);
        apidatas.put("msg","未查询到相关数据");

        RecordSet rs=new RecordSet();


        String requestid=Util.null2String(params.get("requestId"));


        if(requestid==""){
            apidatas.put("status",false);
            apidatas.put("msg","必填参数不允许为空");
            return apidatas;
        }



        String [] res=requestid.split(",");
        String  requestidStr="'0'";
        for (String s:  res) {
            requestidStr+=",'"+s+"'";
        }
        String sql=" select workflowid,REQUESTID,REQUESTNAME,wr.CREATER,hr.WORKCODE,((TO_DATE(wr.CREATEDATE||' '||wr.CREATETIME,'YYYY-MM-DD HH24:MI:SS')-to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS'))* 86400 - 8*3600)*1000 as createdatetime,REQUESTMARK from  workflow_requestbase wr,hrmresource hr " +
                "where hr.id=wr.CREATER  and wr.requestid in("+requestidStr+")";
        rs.execute(sql);
        JSONArray data=new JSONArray();

        while (rs.next()){
            JSONObject qr = new JSONObject();
            qr.put("sys",rs.getPropValue("angel_fna","oa_code"));//源系统
            qr.put("workflowId",rs.getString("workflowid"));//单据类型
            qr.put("requestMark",rs.getString("REQUESTMARK"));//源系统单据编号
            qr.put("requestName",rs.getString("REQUESTNAME"));//源系统单据名称
            qr.put("workCode",rs.getString("WORKCODE"));//申请人（员工号）
            qr.put("createDatetime",rs.getString("createdatetime"));//申请时间
            qr.put("requestId",rs.getString("requestid"));//申请时间
            data.add(qr);
        }

        if(data.size()>0) {
            apidatas.put("data", data);
            apidatas.put("status",true);
            apidatas.put("msg","获取成功");
        }
        return apidatas;
    }



}
